package net.takela.dict.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import net.takela.common.web.model.Model;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author baboy
 * @since 1.0.0 07/12/2016
 */
@Getter
@Setter
public class SysDict extends Model {

    /**
     *
     */
    public static class ValueType{
        /**
         *
         */
        public static String STRING = "String";
        /**
         *
         */
        public static String INT = "Integer";
        /**
         *
         */
        public static String LONG = "Long";
        /**
         *
         */
        public static String FLOAT = "Float";
        /**
         *
         */
        public static String LIST = "List";
        /**
         *
         */
        public static String MAP = "HashMap";
    }
    private static final long serialVersionUID = -7095108879458789231L;
    /**
     *
     */
    private Integer id;
    /**
     *
     */
    private String title;
    /**
     *
     */
    private String module;
    /**
     *
     */
    private String type;
    /**
     *
     */
    private String key;
    /**
     *
     */
    private String note;

    /**
     *
     */
    private Integer createUid;
    /**
     *
     */
    private Date createTime;
    /**
     *
     */
    private Integer updateUid;
    /**
     *
     */
    private Date updateTime;
    /**
     *
     */
    private List<SysDictItem> items;

    /**
     *
     */
    @Getter
    @Setter
    public static class SysDictItem extends Model{
        private static final long serialVersionUID = 4208300314227572060L;
        /**
         *
         */
        private Integer id;
        /**
         *
         */
        private Integer dictId;
        /**
         *
         */
        private String title;
        /**
         *
         */
        private String key;
        /**
         *
         */
        private String value;
        /**
         *
         */
        private String valueType;
        /**
         *
         */
        private String note;
        /**
         *
         */
        private Integer sortNo;
        /**
         *
         */
        private String createUid;
        /**
         *
         */
        private Date createTime;
        /**
         *
         */
        private Date updateUid;
        /**
         *
         */
        private Date updateTime;
        /**
         *
         */
        private Object metadata;

        /**
         *
         * @return 自动识别返回类型数据
         */
        public Object getTypedValue(){
            if (value == null){
                return null;
            }
            Object val = value;
            if (ValueType.INT.equalsIgnoreCase(valueType)){
                val = Integer.parseInt(value);
            }
            if (ValueType.LONG.equalsIgnoreCase(valueType)){
                val = Long.parseLong(value);
            }
            if (ValueType.FLOAT.equalsIgnoreCase(valueType)){
                val = Float.parseFloat(value);
            }
            try {
                if(value.startsWith("[") && ValueType.LIST.equalsIgnoreCase(valueType)){
                    ObjectMapper mapper = new ObjectMapper();
                    val = mapper.readValue(value, ArrayList.class);
                }else if(value.startsWith("{") && ValueType.MAP.equalsIgnoreCase(valueType)){
                    ObjectMapper mapper = new ObjectMapper();
                    val = mapper.readValue(value, HashMap.class);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return val;
        }
    }

}
